<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_Tour extends CI_Model {

    function __construct() {
        parent::__construct();
    }
    
    public function getFoto($id_tour) {
        $sql="select * from foto_tour where id_tour = ? order by urutan desc";
        $query = $this->db->query($sql,array($id_tour));
        return $query->result();
    }
    
    public function getFotoItenary($id_tour) {
        $sql="select * from itenary_tour where id_tour = ? order by urutan desc";
        $query = $this->db->query($sql,array($id_tour));
        return $query->result();
    }

    public function updatePathFoto($id, $pathName) {        
        global $g_base_home;
        $path = $this->db->query("select path_foto from foto_tour where id_foto=?",array($id))->row()->path_foto;        
        if ($path !== "assets/img/tour/default.jpg" && $path != $pathName){            
            unlink($g_base_home."/".$path);
        }
        
        $this->db->query("update foto_tour set path_foto =? where id_foto = ?", array($pathName,$id));
    }
    
    public function updatePathFotoItenary($id, $pathName) {        
        global $g_base_home;
        $path = $this->db->query("select path_foto from itenary_tour where id_itenary=?",array($id))->row()->path_foto;        
        if ($path !== "assets/img/tour/default.jpg" && $path != $pathName){            
            unlink($g_base_home."/".$path);
        }
        
        $this->db->query("update itenary_tour set path_foto =? where id_itenary = ?", array($pathName,$id));
    }
 
    public function hapusFoto($id_foto) {
        global $g_base_home;
        $path = $this->db->query("select path_foto from foto_tour where id_foto=?",array($id_foto))->row()->path_foto;
        $this->db->query("delete from foto_tour where id_foto = ?", array($id_foto));
        if ($path !== "assets/img/tour/default.jpg"){            
            unlink($g_base_home."/".$path);
        }
    }
    
    public function hapusFotoItenary($id_foto) {
        global $g_base_home;
        $path = $this->db->query("select path_foto from itenary_tour where id_itenary=?",array($id_foto))->row()->path_foto;
        $this->db->query("delete from foto_tour where id_itenary = ?", array($id_foto));
        if ($path !== "assets/img/tour/default.jpg"){            
            unlink($g_base_home."/".$path);
        }
    }

    public function tambahFoto($id_tour) {
        $default_foto = "assets/img/tour/default.jpg";
        $urutan = $this->db->query("select max(urutan)+1 as urutan from foto_tour where id_tour = ?",array($id_tour))->row()->urutan;
        if (!$urutan){
            $urutan = 1;
        }
        $is_foto_utama = $this->db->query("select max(is_foto_utama) from foto_tour where id_tour = ?",array($id_tour))->row()->is_foto_utama;
        if ($is_foto_utama){
            $is_foto_utama = 0;
        } else {
            $is_foto_utama = 1;
        }        
        $this->db->query("insert into foto_tour(id_tour,path_foto,urutan,is_foto_utama,hint) values (?,?,?,?,?)", 
                array($id_tour,$default_foto,$urutan,$is_foto_utama,''));
    }
    
    public function tambahFotoItenary($id_tour) {
        $default_foto = "assets/img/tour/default.jpg";
        $urutan = $this->db->query("select max(urutan)+1 as urutan from itenary_tour where id_tour = ?",array($id_tour))->row()->urutan;
        if (!$urutan){
            $urutan = 1;
        }
        
        $this->db->query("insert into itenary_tour(id_tour,subjek,itenary_konten,path_foto,urutan) values (?,?,?,?,?)", 
                    array($id_tour,'-','-',$default_foto,$urutan));
    }
    
    public function get_data_tour($id_tour){
        $sql="select * from data_tour order by id_tour asc";
        $query = $this->db->query($sql,array(1));
        return $query->result();
    }
    
    public function reset_data_tour(){
        $sql="insert into data_hasil_pooling_history(id_tour,id_member,alias_member) select id_tour,id_member,alias_member from data_hasil_pooling";
        $this->db->query($sql);
        $sql="delete from data_hasil_pooling";
        $this->db->query($sql);
    }
}
